package com.sq.partner.manager.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.DictCache;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.dao.impl.XBTSwitchDaoImpl;
import com.sq.partner.manager.mybatis.model.CoreDept;
import com.sq.partner.manager.mybatis.model.DictDto;
import com.sq.partner.manager.mybatis.model.GatewayConfig;
import com.sq.partner.manager.mybatis.model.NlifePlan;
import com.sq.partner.manager.mybatis.model.XbtSwitchDto;
import com.sq.partner.manager.service.IGatewayConfigService;
import com.sq.partner.manager.service.XBTSwitchService;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

/**
 * 
 * 信保通开关
 */
@Controller
@RequestMapping(value = "/XBTSwitch")
public class XBTSwitchController {
	private static Logger logger = LogUtil.getLog(XBTSwitchController.class);

	@Autowired
	private XBTSwitchService xbtSwitchServiceImpl;
	@Autowired
	private  IGatewayConfigService  gatewayConfigService;
	@RequestMapping(value="/toSwitch")
	public String toSwitch(){
		return "/nlifeProduct/xbtSwitch/index";
	}
	
	/**
	 * 分页条件查询出单机构
	 * @param page
	 * @param dept
	 * @param partnerNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryDeptPageByCriteria")
	@ResponseBody
	public Page<CoreDept> queryDeptPageByCriteria(HttpSession session  ,Page<CoreDept> page, CoreDept dept) throws Exception{
		page.setType(dept);
		page.setObj(session.getAttribute(Constants.SESSION_USER));
		page = xbtSwitchServiceImpl.queryDeptPageByCriteria(page);
	
		return page;
	}
	/**
	 * 查询开关列表
	 * @param page
	 * @param dept
	 * @param partnerNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/querySwichList")
	@ResponseBody
	public Page<XbtSwitchDto> querySwichList(HttpSession session ,Page<XbtSwitchDto> page, String dptCde) throws Exception{
		page.setObj(dptCde);
		page = xbtSwitchServiceImpl.querySwichList(page);
		GatewayConfig xbtConfig = gatewayConfigService.queryConfigByName("XBT_DPT");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("XBT_DPT", "");
		if(xbtConfig.getConfigValue().contains((dptCde.substring(0,2)))){
			map.put("XBT_DPT", 1);
		}
		page.setMap(map);
		return page;
	}
	/**
	 * 删除开关
	 *2019/7/29改动：关闭信保通开关应当将该机构的所有信保通保额上下限删除；
	 * @param nlifePlan
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/delXbtSwitch")
	@ResponseBody
	public Result delXbtSwitch(String deptCode) throws Exception{
		try {
			if(StrUtil.isEmpty(deptCode)){
				return Result.error("参数有误！");
			}
			 xbtSwitchServiceImpl.deleteXbtSwitchDto(deptCode);
			 return Result.success("关闭成功");
		} catch (Exception e) {
		/*	logger.info("关闭失败",e);*/
			return Result.error("关闭失败");
		}
	}
	
	/**
	 * 添加开关
	 */
	@RequestMapping(value = "/addXbtSwitch")
	@ResponseBody
	public Result xbtSwitch(XbtSwitchDto xbtSwitchDto) throws Exception{
		boolean flag=xbtSwitchServiceImpl.addXbtSwitchDto(xbtSwitchDto);
		if(flag){
			return Result.success("打开成功");
		}else{
			return Result.error("打开失败");
		}
	}
	
	//修改机构限额
	@RequestMapping(value = "/updateAmountLimit")
	@ResponseBody
	public Result updateAmountLimit(String deptCode,String productCode,String loaninvoiceno,String amountLimit,String denyAmountLimit) throws Exception{
			xbtSwitchServiceImpl.updateAmountLimit(deptCode, productCode, loaninvoiceno,amountLimit,denyAmountLimit);
			return Result.success("更改成功");
		
	}
	
	//根据deptCode查询Risk列表
	@RequestMapping(value = "/queryRiskList")
	@ResponseBody
	public Page<XbtSwitchDto> queryRiskList(HttpSession session ,Page<XbtSwitchDto> page, String dptCde) throws Exception{
		page.setObj(dptCde);
		page = xbtSwitchServiceImpl.queryRiskList(page);
		return page;
	}
	//增加限额信息
	@RequestMapping(value = "/insertAmountLimit")
	@ResponseBody
	public Result insertAmountLimit(String deptCode,String productCode,String loaninvoiceno,String amountLimit,String denyAmountLimit) throws Exception{
		if(StrUtil.isNotEmpty(amountLimit) ){
			Result.error("限额不能为空");
		}
		boolean reslut = xbtSwitchServiceImpl.insertAmountLimit(deptCode, productCode, loaninvoiceno,amountLimit,denyAmountLimit);
		if(reslut == true){
			return Result.success("更改成功");
		}
		else{
			return Result.error("已存在限额");
		}
	}
	
	//回写限额信息
	@RequestMapping(value = "/writeBackThisLimit")
	@ResponseBody
	public Map<String,String> writeBackThisLimit(String productCode , String loaninvoiceno , String deptCode) throws Exception{
		Map<String, String> map = new HashMap<String, String>();
		map.put("productCode", productCode);
		map.put("loaninvoiceno", loaninvoiceno);
		map.put("deptCode", deptCode);
		
		return xbtSwitchServiceImpl.writeBackThisLimit(map);
		
	}
}